package com.xian.tszs.meal.component;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.tencentcloudapi.aai.v20180522.AaiClient;
import com.tencentcloudapi.aai.v20180522.models.TextToVoiceRequest;
import com.tencentcloudapi.aai.v20180522.models.TextToVoiceResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.xian.tszs.meal.utils.UUIDUtils;

/***
 * 语音合成组件
* @Title: SpeechSynthesis.java 
* @author MRC
* @date 2019年5月23日 下午1:20:21 
* @version V1.0
 */
@Component
public class SpeechSynthesis {
	
	
	@Value("${meal.xian.tenter.SecretId}")
	private String secretId;
	
	
	@Value("${meal.xian.tenter.SecretKey}")
	private String secretKey;
	
	public String speechSynth(String text){
		
		String result = null;
		
		 try{
			 
			 	String SessionId = UUIDUtils.getUUID();
			 
	            Credential cred = new Credential(secretId, secretKey);
	            
	            HttpProfile httpProfile = new HttpProfile();
	            httpProfile.setEndpoint("aai.tencentcloudapi.com");

	            ClientProfile clientProfile = new ClientProfile();
	            clientProfile.setHttpProfile(httpProfile);            
	            
	            AaiClient client = new AaiClient(cred, "ap-beijing", clientProfile);
	            
	            String params = "{\"Text\":\""+text+"\",\"SessionId\":\""+SessionId+"\",\"ModelType\":1}";
	            
	            TextToVoiceRequest req = TextToVoiceRequest.fromJsonString(params, TextToVoiceRequest.class);
	            
	            TextToVoiceResponse resp = client.TextToVoice(req);
	            
	            result = resp.getAudio();
	            
	        } catch (TencentCloudSDKException e) {
	                System.out.println(e.toString());
	        }
		return result;
	}
}
